//
//  AMVistorView.swift
//  微博
//
//  Created by zhc on 15/11/23.
//  Copyright © 2015年 zhc. All rights reserved.
//

import UIKit
//协议
protocol AMVistorViewDelegate:NSObjectProtocol{

   func vistorViewLoginClick()


}



class AMVistorView: UIView {
    
    //设置代理属性
    
    //OC中property
    weak var AMVVdelegate:AMVistorViewDelegate?
    
    
    func loginClick(){
       //通知代理
        AMVVdelegate?.vistorViewLoginClick()
    
    
    }
    
    
    

    //swift默认所有的view都能通过xib/storyboard加载
    
    required init?(coder aDecoder:NSCoder){
    
      fatalError("init(coder:)has not been implemented")
    
    
    }
    
    
    override init(frame:CGRect){
      //super是要再设置普通的存储行属性的下面
        super.init(frame: frame)
        backgroundColor = UIColor(white: 237/255, alpha: 1)
        prepareUI()
        
    }
    //首页转转转
    
    func startRotationAnimation(){
     let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = 2*M_PI
        animation.duration = 20
        animation.repeatCount = MAXFLOAT
        
        //加上这个切换到其他地方就会回来还会转
        animation.removedOnCompletion = false
        
        //转起来
        iconView.layer.addAnimation(animation, forKey: "animation")
    
    
    
    
    }
    
    // MARK: - 核心动画开始和暂停
    /// 暂停旋转
    func pauseAnimation() {
        // 记录暂停时间
        let pauseTime = iconView.layer.convertTime(CACurrentMediaTime(), fromLayer: nil)
        
        // 设置动画速度为0
        iconView.layer.speed = 0
        
        // 设置动画偏移时间
        iconView.layer.timeOffset = pauseTime
    }
    
    /// 恢复旋转
    func resumeAnimation() {
        // 获取暂停时间
        let pauseTime = iconView.layer.timeOffset
        
        // 设置动画速度为1
        iconView.layer.speed = 1
        
        iconView.layer.timeOffset = 0
        
        iconView.layer.beginTime = 0
        
        let timeSincePause = iconView.layer.convertTime(CACurrentMediaTime(), fromLayer: nil) - pauseTime
        
        iconView.layer.beginTime = timeSincePause
    }
    
    
    
    
    
    
    
    //设置访客视图内容
    
    func setupVistorView(imageName:String,message:String){
        
        iconView.image = UIImage(named: imageName)
        
        messagelabel.text = message
        homeView.hidden = true
        
        sendSubviewToBack(coverView)
  
    
    }
    
    
    

    //准备UI
    
    private func prepareUI(){
        
        //backgroundColor = UIColor(white: 237/255, alpha: 1)
        
        
        addSubview(iconView)
        addSubview(coverView)
        addSubview(homeView)
        addSubview(messagelabel)
        addSubview(registerButton)
        
        addSubview(loginButton)
        
        
        iconView.translatesAutoresizingMaskIntoConstraints = false
        homeView.translatesAutoresizingMaskIntoConstraints = false
        messagelabel.translatesAutoresizingMaskIntoConstraints = false
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        
        coverView.translatesAutoresizingMaskIntoConstraints=false
        
        
        //iconView
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))

        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        //homeView
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
       addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        //messageView
        addConstraint(NSLayoutConstraint(item: messagelabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 10))
         addConstraint(NSLayoutConstraint(item: messagelabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
      addConstraint(NSLayoutConstraint(item: messagelabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 225))
        //登陆按钮
        
      addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 85))
        
      addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
      addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: messagelabel, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        
      addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messagelabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 10))
        
        //注册按钮
        
        
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 85))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: messagelabel, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messagelabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 10))
        
        
        //遮罩
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        
        
        
        
        
       
        
    
    
    }
    
    //懒加载内容
    
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    
    
    
    private lazy var homeView: UIImageView = UIImageView(image: UIImage(named:
        "visitordiscover_feed_image_house"))
    
    
    private lazy var coverView: UIImageView = {
    
      let image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        
        return UIImageView(image: image)
    
    
    
    }()
    
    
    
    
    private lazy var messagelabel:UILabel = {
    
       let label = UILabel()
       
        label.textColor = UIColor.redColor()
        
        label.text = "关注一些人，看看有神马惊喜"
        label.numberOfLines = 0
        label.sizeToFit()
     
    
       return label
    
    }()
    
    //注册按钮
    private lazy var registerButton :UIButton = {
    
       let button = UIButton()
        button.setBackgroundImage(UIImage(imageLiteral: "common_button_white_disable"), forState: UIControlState.Normal)
        
        button.setTitle("注册", forState: UIControlState.Normal)
       button.setTitleColor(UIColor.redColor(), forState: UIControlState.Normal)

       button.titleLabel?.font = UIFont.systemFontOfSize(14)
       button.sizeToFit()
       return button
    
    }()
    
    private lazy var loginButton :UIButton = {
        
        let button = UIButton()
         button.setBackgroundImage(UIImage(imageLiteral: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitle("登陆", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.redColor(), forState: UIControlState.Normal)
        
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        button.sizeToFit()
        
        
        // 添加点击事件
        button.addTarget(self, action: "loginClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        
        return button
        
        
        
    }()
    
  

}
